package services;

import java.util.Collection;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import repositories.UserGroupRepository;
import domain.Administrator;
import domain.UserGroup;
import forms.UserGroupForm;

@Service
@Transactional
public class UserGroupService {

	// ManagedRepository
	@Autowired
	private UserGroupRepository userGroupRepository;
	@Autowired
	private AdministratorService administratorService;

	// Supporting Services

	// Constructors

	public UserGroupService() {
		super();
	}

	// Simple CRUD methods

	public UserGroup create() {
		UserGroup result;
		
		Assert.isTrue(administratorService.findByPrincipal() instanceof Administrator);
		result = new UserGroup();
		result.setAdministrator(administratorService.findByPrincipal());
		return result;

	}

	public void save(UserGroup userGroup) {
		Assert.notNull(userGroup);
		Assert.isTrue(userGroup.getUsers().isEmpty());
		Assert.isTrue(userGroup.getVacancies().isEmpty());
		Assert.isTrue(userGroup.getAdministrator().equals(administratorService.findByPrincipal()));
		userGroupRepository.save(userGroup);

	}

	public void delete(UserGroup userGroup) {
		Assert.notNull(userGroup);
		Assert.isTrue(userGroup.getAdministrator().equals(administratorService.findByPrincipal()));
		Assert.isTrue(userGroup.getUsers().isEmpty());
		Assert.isTrue(userGroup.getVacancies().isEmpty());
		userGroupRepository.delete(userGroup);

	}

	public Collection<UserGroup> findAll() {
		Collection<UserGroup> result;

		result = userGroupRepository.findAll();

		return result;
	}

	public UserGroup findOne(int userGroupId) {
		Assert.isTrue(userGroupId != 0);
		UserGroup result;
		result = userGroupRepository.findOne(userGroupId);
		Assert.notNull(result);

		return result;
	}

	public UserGroupForm userGroupToForm(UserGroup userGroup) {
		Assert.notNull(userGroup);
		UserGroupForm result;

		result = new UserGroupForm();
		result.setId(userGroup.getId());
		result.setName(userGroup.getName());
		result.setDescription(userGroup.getDescription());

		return result;

	}

	// reconstruct Method

	public UserGroup reconstruct(UserGroupForm userGroupForm) {
		UserGroup result;
		
		Assert.notNull(userGroupForm);
		
		result = create();
		result.setName(userGroupForm.getName());
		result.setDescription(userGroupForm.getDescription());

		return result;
	}

	public UserGroup updateForm(UserGroupForm userGroupForm) {
		Assert.notNull(userGroupForm);
		UserGroup result;
		
		result = findOne(userGroupForm.getId());
		result.setDescription(userGroupForm.getDescription());
		result.setName(userGroupForm.getName());
		return result;

	}

	public Collection<UserGroup> findAvailableGroups(int projectId) {
		Assert.isTrue(projectId != 0);
		Collection<UserGroup> result;
		result = userGroupRepository.findAvailableGroups(projectId);
		Assert.notNull(result);
		return result;
	}

}
